package com.ruoyi.project.module.applicationProperty.controller;

import com.ruoyi.common.support.Convert;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.module.application.domain.Application;
import com.ruoyi.project.module.application.service.IApplicationService;
import com.ruoyi.project.module.applicationProperty.service.IApplicationPropertyService;
import com.ruoyi.project.module.group.domain.Group;
import com.ruoyi.project.module.group.service.IGroupService;
import com.ruoyi.project.module.groupProperty.domain.GroupProperty;
import com.ruoyi.project.module.groupProperty.service.IGroupPropertyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 **/
@Controller
@RequestMapping("/module/applicationPropertyImport")
public class ApplicationPropertyImportController extends BaseController{

    private String prefix = "module/applicationProperty";

    @Autowired
    private IGroupService groupService;

    @Autowired
    private IGroupPropertyService groupPropertyService;

    @Resource
    private IApplicationService applicationService;

    @Resource
    private IApplicationPropertyService applicationPropertyService;

    @RequiresPermissions("module:group:list")
    @GetMapping("/selectConfigGroup/{uniqueId}")
    public String selectConfigGroup(@PathVariable("uniqueId") Long uniqueId, ModelMap mmap)
    {
        Application value = applicationService.selectApplicationById(uniqueId);
        mmap.put("configApplication", value);
//        mmap.put("dictList", groupPropertyService.selectDictTypeAll());
        return prefix + "/selectConfigGroup";
    }

    @RequiresPermissions("module:group:list")
    @PostMapping("/configGroup/list")
    @ResponseBody
    public TableDataInfo selectConfigGroup(Group group)
    {
        startPage();
        List<Group> list = groupService.selectGroupList(group);
        return getDataTable(list);
    }

    @RequiresPermissions("module:group:list")
    @GetMapping("/selectConfigGroupProperty/{uniqueId}")
    public String selectConfigGroupProperty(@PathVariable("uniqueId") Long uniqueId, ModelMap mmap)
    {
        Application value = applicationService.selectApplicationById(uniqueId);
        mmap.put("configApplication", value);
//        mmap.put("dictList", groupPropertyService.selectDictTypeAll());
        return prefix + "/selectConfigGroupProperty";
    }

    @RequiresPermissions("module:group:list")
    @PostMapping("/configGroupProperty/list")
    @ResponseBody
    public TableDataInfo configGroupPropertyList(GroupProperty groupProperty)
    {
        startPage();
        List<GroupProperty> list = groupPropertyService.lookupGroupPropertyList(groupProperty);
        return getDataTable(list);
    }

    @RequiresPermissions("module:group:list")
    @PostMapping("/configGroup/import")
    @ResponseBody
    @Transactional
    public AjaxResult configGroupImport(@RequestParam(value = "configGroupId") Long configGroupId,
                                        @RequestParam(value = "propertyPrefix") String propertyPrefix,
                                        @RequestParam(value = "configApplicationId") Long configApplicationId

    )
    {
        applicationPropertyService.importGroupProperty(configApplicationId,configGroupId,propertyPrefix);
        return toAjax(1);
    }


    @RequiresPermissions("module:group:list")
    @PostMapping("/configGroupProperty/import")
    @ResponseBody
    @Transactional
    public AjaxResult configGroupPropertyImport(@RequestParam(value = "configGroupPropertyId") String configGroupPropertyId,
                                        @RequestParam(value = "propertyPrefix") String propertyPrefix,
                                        @RequestParam(value = "configApplicationId") Long configApplicationId

    )
    {
        //暂时只支持导入一个,因为前缀不同
        applicationPropertyService.importGroupPropertySelective(configApplicationId, Convert.toLongArray(configGroupPropertyId),propertyPrefix);
        return toAjax(1);
    }
}
